﻿using UnityEngine;
using System.Collections;

public class MessageTabBarButton : ObjectBase
{
	Transform m_On;
	Transform m_Off;
	Transform m_Disable;

	MessageTabBar m_tabBar = null;
	public MessageTabBar TabBar
	{
		get { return m_tabBar; }
		set { m_tabBar = value; }
	}

	int m_buttonIndex = 0;
	public int ButtonIndex
	{
		get { return m_buttonIndex; }
		set { m_buttonIndex = value; }
	}

	bool m_IsChecked = false;
	public bool IsChecked
	{
		get { return m_IsChecked; }
		set { m_IsChecked = value; SetState(); OnSelected(); }
	}

	public bool isEnabled 
	{ 
		get { return enabled; } 
		set { enabled = value; SetDisable(); } 
	}

	void Awake()
	{
		m_On = transform.Find("on");
		m_Off = transform.Find("off");       
		m_Disable = transform.Find("disable"); 
		
		SetState();
	}

	void OnPress(bool isPressed)
	{
		if (isEnabled == true && IsChecked == false)
		{
			TabBar.SelectTab(ButtonIndex);
		}
	}

	/*
	void OnPress(bool isPressed)
	{
		if (isEnabled && UICamera.currentTouch != null)
		{           
			if (isPressed)
			{
				
			}
			else if (UICamera.currentTouch.current == gameObject)
			{
				TabBar.SelectTab(ButtonIndex);
				//IsChecked = !IsChecked;
			}            
		}        
	}
	*/
	
	void SetDisable()
	{
		if (m_Disable != null)
			m_Disable.gameObject.SetActive(!isEnabled);
		if (m_On != null)
			m_On.gameObject.SetActive(isEnabled);
		if (m_Off != null)
			m_Off.gameObject.SetActive(isEnabled);     
	}
	
	void SetState()
	{
		m_Off.gameObject.SetActive(!IsChecked);
		m_On.gameObject.SetActive(IsChecked);
	}

	void OnSelected()
	{
		MessageScene sceneManager = GameObject.Find ("SceneManager").GetComponent<MessageScene>();
		switch(TabBar.SelectedIndex)
		{
		case 0:
			Debug.Log ("0");
			sceneManager.SelectTab(MessageScene.TAB_TYPE.MAIL_MESSAGE);
			break;
		case 1:
			Debug.Log ("1");
			sceneManager.SelectTab(MessageScene.TAB_TYPE.BATTLE_MESSAGE);
			break;
		case 2:
			Debug.Log ("2");
			sceneManager.SelectTab(MessageScene.TAB_TYPE.REWARD_MESSAGE);
			break;
		}

	}
}
